<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2008 Thomas Breuss <thomasbreuss@gmx.ch>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

require_once(PATH_tslib.'class.tslib_pibase.php');
require_once(t3lib_extMgm::extPath('zview') . 'class.tx_zview.php');

/**
 * Plugin 'zviewtest' for the 'zviewtest' extension.
 *
 * @author	Thomas Breuss <thomasbreuss@gmx.ch>
 * @package	TYPO3
 * @subpackage	tx_zviewtest
 */
class tx_zviewtest_pi1 extends tslib_pibase
{
	var $prefixId      = 'tx_zviewtest_pi1';		// Same as class name
	var $scriptRelPath = 'pi1/class.tx_zviewtest_pi1.php';	// Path to this script relative to the extension dir.
	var $extKey        = 'zviewtest';	// The extension key.
	var $pi_checkCHash = true;

	private $_view;

	/**
	 * The main method of the PlugIn
	 *
	 * @param	string		$content: The PlugIn content
	 * @param	array		$conf: The PlugIn configuration
	 * @return	The content that is displayed on the website
	 */
	function main($content,$conf)
	{
		$this->conf=$conf;

		// Initializer
		$this->_init();

		// Static text
		$content = $this->_view->render('intro.phtml');
		$this->_view->clearVars();

		// Exmaple with foreach...
		$content .= $this->foreachAction();
		$this->_view->clearVars();

		// Form-Example
		$content .= $this->formAction();
		$this->_view->clearVars();

		// HTML List-Example
		$content .= $this->listAction();
		$this->_view->clearVars();

		// Translation Example
		$content .= $this->_view->render('translate.phtml');
		$this->_view->clearVars();

		// Internal Links
		$this->_view->id = $GLOBALS['TSFE']->page['uid'];
		$content .= $this->_view->render('links.phtml');
		$this->_view->clearVars();

		// Tables Example
		$content .= $this->tableAction();
		$this->_view->clearVars();

		// Images Example
		$content .= $this->_view->render('image.phtml');
		$this->_view->clearVars();
		//t3lib_div::debug($view);

		// Mailto-Links
		$content .= $this->_view->render('mailto.phtml');
		$this->_view->clearVars();

		// Environment
		$content .= $this->_view->render('env.phtml');
		$this->_view->clearVars();

		// Pageinfo
		$content .= $this->_view->render('page.phtml');
		$this->_view->clearVars();

		// RTE
		$this->_view->rteTest1 = isset($this->piVars['rteTest1']) ? $this->piVars['rteTest1'] : 'That is a sample text 1';
		$this->_view->rteTest2 = isset($this->piVars['rteTest2']) ? $this->piVars['rteTest2'] : 'That is a sample text 2';
		$content .= $this->_view->render('rte.phtml');
		$this->_view->clearVars();

		// File info
		$content .= $this->_view->render('fileinfo.phtml');
		$this->_view->clearVars();

		// Feuser
		$content .= $this->_view->render('feuser.phtml');
		$this->_view->clearVars();

		// Typolink
		$content .= $this->_view->render('typolink.phtml');
		$this->_view->clearVars();

		// Extconf

		// Date

		// Flexform

		return $this->pi_wrapInBaseClass($content);

	}

	private function _init()
	{
		$this->pi_setPiVarDefaults();
		$this->pi_loadLL();

		// Only for testing
    	$GLOBALS['TSFE']->set_no_cache();

    	// Make instance of zview
    	$this->_view = t3lib_div::makeInstance('tx_zview');
		$this->_view->setPi($this);
		$this->_view->addScriptPath(t3lib_extMgm::extPath('zviewtest') . 'pi1/scripts/');
	}

	private function foreachAction()
	{
		$books = array(
		    array(
		        'author' => 'J. K. Rowling',
		        'title' => 'Harry Potter and the Deathly Hallows'
		    ),
		    array(
		        'author' => 'Khaled Hosseini',
		        'title' => 'A Thousand Splendid Suns'
		    ),
		    array(
		        'author' => 'Milton Friedman',
		        'title' => 'Free to Choose'
		    ),
		    array(
		    	'author' => 'Jeffrey Toobin',
		    	'title' => 'The Nine: Inside the Secret World of the Supreme Court'
		    ),
		    array(
		    	'author' => 'J. R. R. Tolkien',
		    	'title' => 'The Children of Hurin'
		    )
		);
		$this->_view->books = $books;
		return $this->_view->render('foreach.phtml');
	}

	private function formAction()
	{
		$this->_view->formAction = $_SERVER['REQUEST_URI'];
		$this->_view->text = isset($_POST['text']) ? $_POST['text'] : '' ;
		$this->_view->textarea = isset($_POST['textarea']) ? $_POST['textarea'] : '' ;
		$this->_view->select = isset($_POST['select']) ? $_POST['select'] : '' ;
		$this->_view->radio = isset($_POST['radio']) ? $_POST['radio'] : '' ;
		$this->_view->checkbox = isset($_POST['checkbox']) ? $_POST['checkbox'] : '' ;

		// Validation
		$errors = array();
		if(strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
			if(empty($_POST['text'])) {
				$errors[] = 'Text is required!';
			}
			if(empty($_POST['textarea'])) {
				$errors[] = 'Textarea is required!';
			}
		}
		$this->_view->validationErrors = $errors;

		$submittedValues = array();
		if(empty($errors)) {
			foreach($_POST AS $key=>$value) {
				$submittedValues[] = sprintf('%s => %s', $key, $value);
			}
		}
		$this->_view->submittedValues = $submittedValues;
		return $this->_view->render('form.phtml');
	}

	private function listAction()
	{
		$list1 = array(
	    	'ABC',
	    	'DEF',
	    	'GHI',
	    	'JKL',
	    	'MNO'
		);
		$list2 = array(
	    	'ABC',
	    	'DEF',
	    	'GHI' => array(
	    		'012',
	    		'456',
	    		'789'
	    	),
	    	'JKL',
	    	'MNO'
		);
		$list3 = array(
	    	'Coffee',
	    	'Tea',
	    	'Milk'
		);
		$list4 = array(
	    	'Coffee',
	    	'Tea' => array(
	    		'Black tea',
	    		'Green tea',
	    		'Oolong',
	    		'White tea',
	    		'Yellow tea'
	    	),
	    	'Milk'
		);
		$this->_view->list1 = $list1;
		$this->_view->list2 = $list2;
		$this->_view->list3 = $list3;
		$this->_view->list4 = $list4;
		return $this->_view->render('list.phtml');
	}

	private function tableAction()
	{

        $this->_view->data = array(
        	array('id' => 43, 'name' => 'Peter', 'birthday' => '1991/8/12'),
        	array('id' => 67, 'name' => 'Paul', 'birthday' => '1990/11/17'),
        	array('id' => 89, 'name' => 'Mary', 'birthday' => '1999/7/22'),
        	array('id' => 23, 'name' => 'Tom', 'birthday' => '1995/5/12'),
        	array('id' => 25, 'name' => 'Sue', 'birthday' => '1992/6/21')
        );
		$this->_view->columns = array(
			array('key' => 'id', 'label' => 'ID'),
			array('key' => 'name', 'label' => 'Name'),
			array('key' => 'birthday', 'label' => 'Birthday')
		);
		$this->_view->fields = array(
			array('key' => 'id'),
			array('key' => 'name'),
			array('key' => 'birthday')
		);
		return $this->_view->render('table.phtml');
	}


}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zviewtest/pi1/class.tx_zviewtest_pi1.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zviewtest/pi1/class.tx_zviewtest_pi1.php']);
}

?>